part of 'component.dart';

class ToggleItem extends StatefulWidget {
  const ToggleItem({
    Key? key,
    this.text,
    this.child,
    this.selectedColor,
    this.unSelectedColor,
    this.isSelected = false,
    this.onChanged,
    this.space,
  }) : super(key: key);
  final bool isSelected;
  final String? text;
  final Widget? child;
  final Color? selectedColor;
  final Color? unSelectedColor;
  final void Function(bool)? onChanged;
  final double? space;
  @override
  State<ToggleItem> createState() => _ToggleItemState();
}

class _ToggleItemState extends State<ToggleItem> {
  bool isSelected = false;
  @override
  void initState() {
    isSelected = widget.isSelected;
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (widget.onChanged == null) {
      isSelected = widget.isSelected;
    }
    var col = isSelected
        ? (widget.selectedColor ?? Colors.blue)
        : (widget.unSelectedColor ?? const Color(0xff999999));
    return InkWell(
      onTap: widget.onChanged != null
          ? () {
              isSelected = !isSelected;
              widget.onChanged!(isSelected);
            }
          : null,
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 6.r),
        child: Row(
          children: [
            Icon(
              isSelected ? Icons.check_circle_rounded : Icons.circle_outlined,
              color: col,
            ),
            if (widget.child != null)
              Row(
                children: [
                  SizedBox(
                    width: widget.space ?? 6,
                  ),
                  widget.child!
                ],
              )
            else if (widget.text != null)
              Row(
                children: [
                  SizedBox(
                    width: widget.space ?? 6,
                  ),
                  Text(
                    widget.text!,
                    style: TextStyle(color: col, fontSize: 15),
                  )
                ],
              )
          ],
        ),
      ),
    );
  }
}
